package com.obas.component.tree.treeclass {
   import flash.events.MouseEvent;

   import mx.controls.CheckBox;
   import mx.controls.treeClasses.TreeItemRenderer;
   import mx.controls.treeClasses.TreeListData;

   public class CheckboxTreeItemRenderer extends TreeItemRenderer {
      private static const CONTROL_GAP:int = 2;
      private var _checkBox:CheckBox;

      private var _stateField:String = "state";
      private var _stateFieldChanged:Boolean;
      public function get stateField():String {
         return _stateField;
      }

      public function set stateField(value:String):void {
         if(_stateField == value){
            return;
         }
         _stateField = value;
         _stateFieldChanged = true;
         invalidateProperties();
      }

      override protected function createChildren():void {
         super.createChildren();

         if (_checkBox == null) {
            _checkBox = new CheckBox();
            _checkBox.addEventListener(MouseEvent.CLICK, onCheckboxClick);

            this.addChild(_checkBox);
         }
      }

      private function onCheckboxClick(event:MouseEvent):void {
         updateItemState(_checkBox.selected);
      }

      private function updateItemState(state:Boolean):void {
         var item:Object = this.data;

         if (item == null) {
            return;
         }

         if (item[_stateField] != null) {
            item[_stateField] = state;
         }
      }

      override protected function commitProperties():void {
         super.commitProperties();

         if (this.data != null) {
            var previousState:Boolean = _checkBox.selected;
            var currentState:Boolean = itemState();

            if (previousState != currentState) {
               _checkBox.selected = currentState;
            }

            if (data.hasOwnProperty("readOnly")) {
               _checkBox.visible = !data.readOnly;
            }
         }
      }

      private function itemState():Boolean {
         var item:Object = this.data;
         var state:Boolean = false;

         if (item == null) {
            return state;
         }
         state = item[_stateField];

         return state;
      }

      override protected function updateDisplayList(unscaledWidth:Number,
                                                    unscaledHeight:Number):void {
         super.updateDisplayList(unscaledWidth, unscaledHeight);

         var startx:Number;
         var verticalAlign:String = this.getStyle("verticalAlign");
         var iconPlacement:Boolean = true;

         if(this.icon && iconPlacement == false){
            startx = this.icon.x + this.icon.measuredWidth;
         } else if (this.disclosureIcon != null) {
            startx = this.disclosureIcon.x + this.disclosureIcon.width;
         } else {
            startx = (this.data != null) ? (listData as TreeListData).indent : 0;
         }

         if (_checkBox != null) {
            _checkBox.x = startx + CONTROL_GAP;
            startx = _checkBox.x + _checkBox.measuredWidth;
            _checkBox.setActualSize(_checkBox.measuredWidth, _checkBox.measuredHeight);
         }

         if (this.icon && iconPlacement) {
            icon.x = startx + CONTROL_GAP;
            startx = icon.x + icon.measuredWidth;
         }

         this.label.x = startx + CONTROL_GAP;
         this.label.setActualSize(unscaledWidth - startx, label.measuredHeight);
         startx = label.x + label.measuredWidth;

         if (verticalAlign == "top") {
            _checkBox.y = 0;
            this.label.y = 0;
         } else if (verticalAlign == "bottom") {
            _checkBox.y = unscaledHeight - _checkBox.height + CONTROL_GAP;
            this.label.y =
               unscaledHeight - this.label.getExplicitOrMeasuredHeight() + CONTROL_GAP;
         } else {
            _checkBox.y = (unscaledHeight - _checkBox.height) / 2;
            this.label.y =
               (unscaledHeight - this.label.getExplicitOrMeasuredHeight()) / 2;
         }

      }
   }
}